package com.asiainfo.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.asiainfo.bean.AuthorityBean;
import com.asiainfo.bean.RoleBean;
import com.asiainfo.bean.UserBean;
import com.asiainfo.dao.UserMapper;

/**
 * 用户拥有的权限
 * @author luhf
 * @date 2013-1-20 下午1:53:34
 */
public class MyUserDetailServiceImpl implements UserDetailsService{
	
	private static final Logger log=LoggerFactory.getLogger(MyUserDetailServiceImpl.class);
	
	@Autowired
	@Qualifier("mapper.userMapper")
	private UserMapper userMapper;

	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException{
		log.debug("username is {} "+username);
		//取得用户的权限
		UserBean userBean=userMapper.getUserByName(username);
		if(userBean==null){
			throw new UsernameNotFoundException(username);
		}
		Collection<GrantedAuthority> grantedAuths=obtionGrantedAuthorities(userBean);

		boolean enables=true;
		boolean accountNonExpired=true;
		boolean credentialsNonExpired=true;
		boolean accountNonLocked=true;

		// 封装成spring security的user
		User userdetail=new User(userBean.getUserAccount(),userBean.getUserPassword(),enables,accountNonExpired,credentialsNonExpired,accountNonLocked,grantedAuths);
		return userdetail;
	}

	// 取得用户的权限
	private Collection<GrantedAuthority> obtionGrantedAuthorities(UserBean userBean){
//		Set<GrantedAuthority> authSet=new HashSet<GrantedAuthority>();
//		
//		Set<RoleBean> roleBeans=userBean.getRole();
//		for(RoleBean role:roleBeans){
//			Set<AuthorityBean> authorityBeans=role.getAuthorityBean();
//			for(AuthorityBean authorityBean:authorityBeans){
//				authSet.add(new SimpleGrantedAuthority(authorityBean.getAuthName()));
//			}
//		}
//		return authSet;
		List<AuthorityBean> authoritys= new ArrayList<AuthorityBean>();
		
		
		Set<RoleBean> roles = userBean.getRole();
		for (RoleBean roleBean : roles) {
			Set<AuthorityBean> authorityBeans = roleBean.getAuthorityBean();
			for (AuthorityBean authorityBean : authorityBeans) {
				authoritys.add(authorityBean);
			}
		}
		
		Set<GrantedAuthority> authSet = new HashSet<GrantedAuthority>();
		for (AuthorityBean authorityBean : authoritys) {
			authSet.add(new SimpleGrantedAuthority(authorityBean.getAuthName()));
		}
		return authSet;
	}
}
